<template>
  <div>
    <bread second="商品管理" third="商品分类"></bread>
    <!-- <el-card class="box-card" style="margin-top:15px">
      <el-button type="primary">添加角色</el-button>
      <el-table :data="tableData" border style="width: 100%;margin-top:15px">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="cat_name" label="分类名称"></el-table-column>
        <el-table-column prop="cat_pid" label="是否有效">
          <template slot-scope="scoped">
            <i
              class="el-icon-success"
              v-if="scoped.row.cat_pid == 0"
              style="color:#90ee90"
            ></i>
            <i class="el-icon-circle-close" v-else></i>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template slot-scope="scoped">
            <el-tag v-if="scoped.row.cat_level == 0">一级</el-tag>
            <el-tag v-else-if="scoped.row.cat_level == 1" type="success"
              >二级</el-tag
            >
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="操作"></el-table-column>
      </el-table>
    </el-card>-->

    <el-card class="box-card" style="margin-top:15px">
      <el-button type="primary" @click="addHandler" style="margin-bottom:15px">添加分类</el-button>
      <zk-table
        :data="tableData"
        :columns="columns"
        :expand-type="false"
        :selection-type="false"
        :show-index="true"
        index-text="#"
        :border="props.border"
      >
        <template slot="cat_id" slot-scope="scoped">{{ scoped.row.cat_id }}</template>
        <template slot="cat_pid" slot-scope="scoped">
          <i class="el-icon-success" v-if="scoped.row.cat_pid == 0" style="color:#90ee90"></i>
          <i class="el-icon-circle-close" v-else></i>
        </template>
        <template slot="cat_level" slot-scope="scoped">
          <el-tag v-if="scoped.row.cat_level == 0">一级</el-tag>
          <el-tag v-else-if="scoped.row.cat_level == 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
      </zk-table>

      <div class="block" style="margin-top:10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="cateObj.pagenum"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="cateObj.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="roleForm">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="form.cat_name" autocomplete="off" style="width:500px"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <div class="block">
            <el-cascader
              v-model="value"
              :options="options"
              :props="cascaderProps"
              @change="handleChange"
              :clearable="true"
              change-on-select
              ref="cascaderForm"
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addgoodsCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
/* eslint-disable */
import { cateList_api, addCate_api } from '@/api/index.js'
export default {
  data() {
    return {
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 表格数据
      tableData: [],
      // 表格标题数据
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        {
          label: '分类ID',
          prop: 'cate_id',
          type: 'template',
          template: 'cat_id',
          width: '150px'
        },

        {
          label: '是否有效',
          prop: 'cat_pid',
          type: 'template',
          template: 'cat_pid',
          width: '150px'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'cat_level',
          width: '150px'
        },
        { label: '操作', prop: 'cate_id', type: 'template', template: 'cat_id' }
      ],
      // 显示表格右边框
      props: {
        border: true
      },
      // 获取商品分类
      cateObj: {
        type: [1, 2, 3],
        pagenum: 1,
        pagesize: 5
      },
      dialogFormVisible: false,
      total: null,
      // 添加分类数据
      form: {
        cat_id: '',
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 级联选择器
      value: [], // 绑定用户选择的分类值
      options: [], // 配置级联菜单中数据如何展示
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        // checkStrictly: true
      }
    }
  },
  created() {
    this.cateList()
  },
  methods: {
    handleChange(value) {
      // console.log(value, '11111')
      this.$refs.cascaderForm.dropDownVisible = false
    },
    // 获取商品分类列表
    async cateList() {
      const { data: res } = await cateList_api(this.cateObj)
      this.tableData = res.data.result
      this.total = res.data.total
    },
    // 添加商品分类
    // 商品分类
    async addHandler() {
      this.dialogFormVisible = true
      const { data: res } = await cateList_api({ type: [1, 2] })
      // console.log(res, res.data.result, 'mm')
      this.options = res.data

    },
    // 增加分类
    addgoodsCate() {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) {
          return
        } else {
          // 如果用户选择了父级分类
          if (value.length > 0) {
            //则将数组中的最后一项设置为父级分类
            this.form.cat_pid = value[value.length - 1]
            //level也要跟着发生变化
            this.form.cat_level = value.length

            return
          } else {
            this.form.cat_pid = 0
            this.form.cat_level = 0
          }
          const { data: res } = await addCate_api(this.form)
          console.log(res, 0)
          if (res.meta.status == 201) {
            this.$message({
              message: res.meta.msg,
              type: 'success'
            })
            this.cateList()
            this.dialogFormVisible = false
            this.form.cat_pid = ''
            this.form.cat_name = ''
            this.form.cat_level = ''
            // this.$refs.roleForm.resetFields()
          }
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.cateObj.pagesize = val
      this.cateList()
      // console.log(`每页 ${val} 条`, val, '00000')
    },
    handleCurrentChange(val) {
      this.cateObj.pagenum = val
      this.cateList()
      // console.log(`当前页: ${val}`, val)
    }
  }
}
</script>
<style lang="less" scoped></style>
